package com.cynteka.cyfoman.core.config;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import java.util.regex.Pattern;

/**
 * User: Sergey
 * Date: 25.07.11
 * Time: 14:12
 */
@XmlRootElement
public class Folder2DBItem {
    private String regexp;
    private transient Pattern pattern;
    private List<String> regexpVarnames;
    private List<ActionQuery> queries;

    public Folder2DBItem() {
    }

    public Folder2DBItem(String regexp, List<String> regexpVarnames, List<ActionQuery> queries) {
        this.regexp = regexp;
        this.regexpVarnames = regexpVarnames;
        this.queries = queries;
    }

    @XmlElement
    public String getRegexp() {
        return regexp;
    }

    public void setRegexp(String regexp) {
        this.regexp = regexp;
        recompile();
    }

    @XmlElement
    public List<String> getRegexpVarnames() {
        return regexpVarnames;
    }

    public void setRegexpVarnames(List<String> regexpVarnames) {
        this.regexpVarnames = regexpVarnames;
    }

    private void recompile() {
        pattern = Pattern.compile(regexp);
    }

    @XmlElement
    public List<ActionQuery> getQueries() {
        return queries;
    }

    public void setQueries(List<ActionQuery> queries) {
        this.queries = queries;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Folder2DBItem that = (Folder2DBItem) o;

        if (queries != null ? !queries.equals(that.queries) : that.queries != null) return false;
        if (regexp != null ? !regexp.equals(that.regexp) : that.regexp != null) return false;
        if (regexpVarnames != null ? !regexpVarnames.equals(that.regexpVarnames) : that.regexpVarnames != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = regexp != null ? regexp.hashCode() : 0;
        result = 31 * result + (regexpVarnames != null ? regexpVarnames.hashCode() : 0);
        result = 31 * result + (queries != null ? queries.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Folder2DBItem{" +
                "regexp='" + regexp + '\'' +
                ", regexpVarnames=" + regexpVarnames +
                ", queries=" + queries +
                '}';
    }

    public Pattern getPattern() {
        return pattern;
    }
}
